import { Controller } from "../../mvc/core/Controller";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
import { Game } from "../Game";
import { StatusType } from "../rules/StatusType";
import { AIManager } from "../scene/ai/AIManager";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { SkillProgress } from "../view/common/SkillProgress";
import { GameController } from "../mvc/GameController";
import { AIType } from "../scene/ai/AIType";
import { AutoFightRule } from "../rules/AutoFightRule";
import { TaskTargetInfo } from "../model/TaskTargetInfo";
import { IEntity } from "../scene/player/IEntity";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { DataEvent } from "../events/DataEvent";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { PlayerEvent } from "../scene/events/PlayerEvent";
type int = number;
//class StatusController
    
    export  class StatusController extends Controller
    {
       

        /* internal  */startGuardTimer(): void
        {
            if (this._guardTimer == null) 
            {
                this._guardTimer = new SecTimer(this._guardDelay);
                this._guardTimer.addListener(TimerType.ENTERFRAME, this.onGuardTimerHandler);
            }
            this._guardTimer.delay = this._guardDelay;
            if (!this._guardTimer.running) 
            {
                this._guardTimer.start();
            }
            return;
        }

        /* internal  */onGuardTimerHandler(arg1: SecTimer): void
        {
            var loc1=null;
            var loc2=null;
            if (!Game.isSceneInit()) 
            {
                return;
            }
            if (cache.status.currentStatus != StatusType.AutoPath) 
            {
                if (cache.status.currentStatus != StatusType.AutoFight) 
                {
                    this.stopGuardTimer();
                }
                else if (!AIManager.isCheckBossTimer() && RolePlayer.instance.isCanControlAttack) 
                {
                    if (!RolePlayer.instance.isAttackAction && !RolePlayer.instance.collecting && !SkillProgress.instance.running && !RolePlayer.instance.isWorking && !RolePlayer.instance.isMountWorking) 
                    {
                        loc1 = GameController.scene.selectEntity;
                        if (!(loc1 == null) && !loc1.isDead) 
                        {
                            if (this.isCanReach(loc1)) 
                            {
                                AIManager.onAIControl(loc1, AIType.AI_AutoFight);
                                return;
                            }
                        }
                        if (AutoFightRule.autoFightCrossMap) 
                        {
                            loc2 = cache.scene.getNearlyPassPointTarget(false);
                            if (loc2 == null) 
                            {
                                loc2 = cache.scene.getNearlyPassPointTarget(true);
                            }
                            if (loc2 != null) 
                            {
                                AIManager.onAutoPathAIControl(loc2, AIType.AI_AutoFight);
                                return;
                            }
                        }
                        AIManager.onAIControl(null, AIType.AI_AutoFight);
                    }
                }
            }
            else if (!SkillProgress.instance.running && !RolePlayer.instance.isMove) 
            {
                if (AIManager.lastTarget && AIManager.lastTarget is TaskTargetInfo && !((AIManager.lastTarget as TaskTargetInfo).x == RolePlayer.instance.currentPoint.x) && !((AIManager.lastTarget as TaskTargetInfo).y == RolePlayer.instance.currentPoint.y)) 
                {
                    AIManager.onAutoPathAIControl(AIManager.lastTarget);
                }
            }
            return;
        }

        /* internal  */isCanReach(arg1: IEntity): Boolean
        {
            if (!arg1) 
            {
                return false;
            }
            var loc1=GameMapUtil.getTilePoint(arg1.x, arg1.y);
            var loc2=new Object /* flash.geom.Point */(RolePlayer.instance.targetTilePoint.x, RolePlayer.instance.targetTilePoint.y);
            if (loc2.x == loc1.x && loc2.y == loc1.y) 
            {
                return true;
            }
            var loc3;
            if ((loc3 = Game.scene.findPath(loc2.x, loc2.y, loc1.x, loc1.y, 5000, true)) && loc3.length > 0) 
            {
                return true;
            }
            return false;
        }

        /* internal  */stopGuardTimer(): void
        {
            if (this._guardTimer && this._guardTimer.running) 
            {
                this._guardTimer.stop();
                this._guardTimer.isDelete = false;
            }
            return;
        }

        /* internal  */onAutoFightBeginHandler(arg1: DataEvent): void
        {
            AutoFightRule.fightAtPoint = false;
            if (AutoFightRule.autoFightPassMap == true) 
            {
                this.onAutoPathBeginHandler(null);
                return;
            }
            if (cache.status.currentStatus == StatusType.AutoFight) 
            {
                return;
            }
            cache.status.currentStatus = StatusType.AutoFight;
            RolePlayer.instance.setRoleStatus(cache.status.currentStatus);
            this.startGuardTimer();
            return;
        }

        /* internal  */onAutoFightEndHandler(arg1: DataEvent): void
        {
            AutoFightRule.flag = 0;
            if (cache.status.currentStatus == StatusType.AutoFight) 
            {
                cache.status.currentStatus = 0;
                this.hideStatus();
            }
            return;
        }

        /* internal  */onAutoPathBeginHandler(arg1: DataEvent): void
        {
            if (cache.status.currentStatus == StatusType.AutoPath) 
            {
                return;
            }
            if (!cache.pack.mountsPackCache.hadCallMount && !RolePlayer.instance.isFlying && GameMapUtil.isCanCallMount()) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.KeyControllMounts));
            }
            cache.status.currentStatus = StatusType.AutoPath;
            RolePlayer.instance.setRoleStatus(cache.status.currentStatus);
            this.startGuardTimer();
            RolePlayer.instance.addEventListener(PlayerEvent.Exit_Fight, this.onPlayerExitFightHandler);
            return;
        }

        /* internal  */onAutoPathEndHandler(arg1: DataEvent): void
        {
            if (cache.status.currentStatus == StatusType.AutoPath) 
            {
                cache.status.currentStatus = 0;
                this.hideStatus();
            }
            RolePlayer.instance.removeEventListener(PlayerEvent.Exit_Fight, this.onPlayerExitFightHandler);
            return;
        }

        /* internal  */onPlayerExitFightHandler(arg1: Object /* flash.events.Event */): void
        {
            if (cache.status.currentStatus != StatusType.AutoPath) 
            {
                return;
            }
            if (!cache.pack.mountsPackCache.hadCallMount && !RolePlayer.instance.isFlying && GameMapUtil.isCanCallMount()) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.KeyControllMounts));
            }
            return;
        }

        public hideStatus(): void
        {
            RolePlayer.instance.setRoleStatus(cache.status.currentStatus);
            return;
        }

        public stageResize(): void
        {
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.BeginToFight, this.onAutoFightBeginHandler);
            Dispatcher.addEventListener(EventName.BeginToFightShortcut, this.onAutoFightBeginHandler);
            Dispatcher.addEventListener(EventName.OverFight, this.onAutoFightEndHandler);
            Dispatcher.addEventListener(EventName.AutoPathBegin, this.onAutoPathBeginHandler);
            Dispatcher.addEventListener(EventName.AutoPathEnd, this.onAutoPathEndHandler);
            return;
        }

        private /* var */_guardTimer: SecTimer;

        private /* var */_guardDelay: int=3;
    }
